package com.woniu.microchargeserver.chargemag.other.projectserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniu.microchargeserver.chargemag.other.projectserver.service.IProjectService;
import com.woniu.microchargeserver.common.bean.dtobean.SpaceInfoTreeTwoDto;
import com.woniu.microcommonsserver.entity.BuildInfoEntity;
import com.woniu.microcommonsserver.entity.SpaceInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: hejie
 * @date: 2021/1/30 7:57
 * @description:
 */
@RequestMapping("/project")
@RestController
public class ProjectController {

    @Resource
    private IProjectService projectServiceImpl;




    /**房间树：某栋楼和 该楼下面的所有房间--这对对象的List
     * 根据“build_info中的id”查询属于该build的所有building下的所有房间(如“水青花都小区-11号楼-5单元-8层-802”，即rank为4的SpaceInfoEntity)
     * 如3号楼
     * @author hejie
     * @param bId：build_info中的id
     * @return
     */
    @GetMapping(value="/findSpaceInfoTreeTwoByBid", produces = {"application/json;charset=utf-8"})
    public String findSpaceInfoTreeTwoByBid(Long bId){
        List<SpaceInfoTreeTwoDto> houseTreeTwo=new ArrayList<>();

        //1.根据“build_info中的id”查询属于该build的所有building(如3号楼，即rank为1的SpaceInfoEntity)
        List<SpaceInfoEntity> buildings = projectServiceImpl.findBuildingByBid(bId);


        //2.根据“build_info中的id”、rank为1的id  查询属于该build某个building下的所有房间(如“水青花都小区-11号楼-5单元-8层-802”，即rank为4的SpaceInfoEntity)
        //下面的building：（rank为1的即rank为4的SpaceInfoEntity），如 3号楼
        for (SpaceInfoEntity building: buildings) {
            //dto数据对象：含有一个 rank为1 和 rank为4的 SpaceInfoEntity
            SpaceInfoTreeTwoDto spaceInfoTreeTwoDto=new SpaceInfoTreeTwoDto();
            //查询该 号楼下面的所有room房间（即rank为4的SpaceInfoEntity），如“水青花都小区-11号楼-5单元-8层-802”
            List<SpaceInfoEntity> houses = projectServiceImpl.findRoomByBidAndBuildingId(bId,building.getId(),building.getAbbreviation());

            //重要：
            ///把全称fullname 分割之后，赋值到简称abbreviation中=========
            for (int i=0; i<houses.size(); i++){
                //获取room的全称：如 “水青花都小区-11号楼-5单元-8层-802”
                String fullname= houses.get(i).getFullname();
                String[] names=  fullname.split("-");
                //下面的“-2”是“号楼”或“单元两个字的长度
                String houseName= names[1].substring(0,names[1].length()-2) +"-"
                        + names[2].substring(0,names[2].length()-2) +"-"
                        + names[4];
                houses.get(i).setAbbreviation( houseName );
            }

            //本来给下一行就可以。但是前端要求第2~4行
            spaceInfoTreeTwoDto.setBuilding(building);

            spaceInfoTreeTwoDto.setId(building.getId());
            spaceInfoTreeTwoDto.setRank(building.getRank());
            spaceInfoTreeTwoDto.setAbbreviation(building.getAbbreviation());

            spaceInfoTreeTwoDto.setChildren(houses);

            //添加
            houseTreeTwo.add(spaceInfoTreeTwoDto);
        }

        Long strRoomId=houseTreeTwo.get(0).getChildren().get(0).getId();
        return JSONObject.toJSONString(new ResponseMsg(200,strRoomId+"-"+"查询2层房间树成功",houseTreeTwo));
    }




    /**
     * 根据“build_info中的id”、rank为1的id  查询属于该build某个building下的所有房间(如“水青花都小区-11号楼-5单元-8层-802”，即rank为4的SpaceInfoEntity)
     * @author hejie
     * @param bId：build_info中的id。
     * @param buildingId 在space_info中rank为1，如“13号楼”
     * @param buildingAbbreviation  space_info中rank为1的简称，如“13号楼”
     * @return
     */
    @GetMapping(value="/findHouseByBidAndBuildingId", produces = {"application/json;charset=utf-8"})
    public String findRoomByBidAndBuildingId(Long bId,Long buildingId,String buildingAbbreviation){
        List<SpaceInfoEntity> houses = projectServiceImpl.findRoomByBidAndBuildingId(bId,buildingId,buildingAbbreviation);

        return JSONObject.toJSONString(new ResponseMsg(200,"查询成功",houses));
    }

    /**
     * 根据“build_info中的id”查询属于该build的所有building(如3号楼，即rank为1的SpaceInfoEntity)
     * 如3号楼
     * @author hejie
     * @param bId：build_info中的id
     * @return
     */
    @GetMapping(value="/findBuildingByBid", produces = {"application/json;charset=utf-8"})
    public String findBuildingByBid(Long bId){
        List<SpaceInfoEntity> buildings = projectServiceImpl.findBuildingByBid(bId);

        return JSONObject.toJSONString(new ResponseMsg(200,"查询成功",buildings));
    }

    /**
     * 查询所有的build：如宏泰大厦
     * @author hejie
     * @return
     */
    @GetMapping(value="/findAllBuild", produces = {"application/json;charset=utf-8"})
    public String findAllBuild(){
        List<BuildInfoEntity> builds = projectServiceImpl.findAllBuild();

        return JSONObject.toJSONString(new ResponseMsg(200,"查询成功",builds));
    }

    /**
     * 查询1个房间的id（rank为4）：13（为了给roomId初始化）
     * @author hejie
     * @return
     */
    @GetMapping(value="/findAId", produces = {"application/json;charset=utf-8"})
    public ResponseMsg findAId(){
        Long id = projectServiceImpl.findAId();

        return new ResponseMsg(200,"查询成功",id);
    }

}
